<?php


namespace app\admin\model;


use think\Db;
use think\Session;

class OrderPayLogModel extends BaseModel
{
    protected $name='order_pay_log';

    protected $autoWriteTimestamp = 'int';
    protected $updateTime = false;
    protected $createTime = 'add_time';



    // 添加数据
    public static function doDataAdd($pay_sn,$other_id,$customer_id,$tomb_id,$tomb_num,$handle,$total_price,$type,$itype,$buyer_name='',$buyer_phone=''){
        $pay_ins = [
                'type'=>$type,
                'itype'=>$itype,
                'other_id'=>$other_id,
                'order_id'=>0 ,
                'pay_sn'=>$pay_sn,// 'DJ'.date('YmdHi'),
                'customer_id'=>$customer_id,
                'tomb_id'=>$tomb_id,
                'tomb_num'=>$tomb_num,
                'deal_price'=>$total_price,//
                'total_price'=>$total_price,//
                'handle'=>$handle,//付款人
                'pay_type'=>'',//付款方式
                'real_name'=>$buyer_name,//付款人
                'real_phone'=>$buyer_phone,//付款人
                'status'=> 0,// 付款方式
            ];
       return self::create($pay_ins);
    }

    // 没有结算的信息 墓位相关信息清除
    public  function doRefreshUpdataDelete($pay_id){
        $this->startTrans();
        try {
            $pay_info = $this->alias('pl')->field('pl.*,ci.is_dy_mz')
                ->join('dj_customer_info ci','pl.customer_id = ci.id')
                ->where(['pl.id'=>$pay_id])->find();
            if ($pay_info['is_dy_mz']==0){
                if ($pay_info['type']==4){
                    OrderGoodsModel::where(['pay_id'=>$pay_id])->delete();
                    OrderPayLogModel::where(['id' => $pay_id])->delete();
                }else {
                    if ($pay_info['type'] == 1) {
                        PayDepositModel::where(['id' => $pay_info['other_id']])->delete();
                    } elseif ($pay_info['type'] == 2) {
                        if ($pay_info['itype'] == 1) {
                            ServiceDepositModel::where(['pay_id' => $pay_id])->delete();
                            OrderDepositModel::where(['order_id' => $pay_info['order_id']])->delete();
                        }
                    }
                    OrderModel::where(['order_id' => $pay_info['order_id']])->delete();
                    OrderGoodsModel::where(['order_id' => $pay_info['order_id']])->delete();
                    CustomerInfoModel::where(['id' => $pay_info['customer_id']])->delete();
                    OrderPayLogModel::where(['id' => $pay_id])->delete();
                    TombareaRowColModel::update(['col_status' => 1], ['colID' => $pay_info['tomb_id']]);
                    deldir('upload/archives/'.$pay_info['tomb_num']);//清空文件
                }
                $this->commit();
                return true;
            }else{
                $this->rollback();
                $this->error = '重置失败！请到售后进行退墓。';
                return false;
            }
        } catch (\Exception $exception) {
            $this->rollback();
            $this->error = '重置失败！';
            return false;
        }
    }
    //退款 清除信息
    public  function dorefundUpdataAndDelete($pay_id)
    {
        $this->startTrans();
        try {
            $pay_refund_info = PayRefundModel::where(['id'=>$pay_id])->find();
            $pay_info = OrderPayLogModel::where(['id'=>$pay_refund_info['pay_id']])->find();

            $order_id = $pay_info['order_id'];
            $tomb_id     = $pay_info['tomb_id'];
            $customer_id = $pay_info['customer_id'];
            $is_delete['is_delete']=1;
            OrderModel::update($is_delete,['order_id'=>$order_id]);
            OrderGoodsModel::update($is_delete,['order_id'=>$order_id]);
            CustomerInfoModel::update($is_delete,['id'=>$customer_id]);
            TombareaRowColModel::update(['col_status'=>1],['colID'=>$tomb_id]);
            $up_pay=[
                'refund_approval_status'=>2,
                'refund_state'=>2,
                'refund_time'=>time(),
                'refund_user'=>cmf_get_current_admin_id(),
            ];
            OrderPayLogModel::update($up_pay,['id'=>$pay_refund_info['pay_id']]);
            PayRefundModel::update(['refund_time'=>time(),'refund_state'=>2,'refund_user'=>cmf_get_current_admin_id(),'is_delete'=>1],['id'=>$pay_id]);
            $this->commit();
            return true;
        } catch (\Exception $exception) {
            $this->rollback();
            $this->error = '添加失败';
            return false;
        }
    }


    public function feil_delf($pay_id){
        $this->startTrans();
        try {
            $pay_info = $this->where(['id' => $pay_id])->find();
            $other_id = $pay_info['other_id'];
            $order_id = $pay_info['order_id'];
            $type = $pay_info['type'];
            $itype = $pay_info['itype'];
            $customer_id = $pay_info['customer_id'];
            $is = $this->where(['id' => $pay_id])->delete();
            if ($type == 1) {
            } elseif ($type == 2) {
            } elseif ($type == 3) {
                switch ($itype) {
                    case 3:
                        ArchivesDeceasedModel::update(['buried_id' => 0, 'buied_status' => 0], ['buried_id' => $other_id]);
                        ServiceBuriedModel::where(['pay_id' => $pay_id])->delete();
                        OrderGoodsModel::where( ['buried_id'=>$other_id])->delete();
                        Db::name('pay_project')->where(['pay_id' => $pay_id])->delete();
                        break;
                    case 4:
                        ServiceWeibaoModel::where(['id' => $other_id])->delete();
                        MaintenanceProjectModel::where(['wb_id' => $other_id])->delete();
                        break;
                    case 5:
                        break;
                    case 6:
                        CarveModel::where(['id' => $other_id])->delete();
                        Db::name('carve_image')->where(['carve_id' => $other_id])->delete();
                        Db::name('carve_gold')->where(['carve_id' => $other_id])->delete();
                        Db::name('carve_insc')->where(['carve_id' => $other_id])->delete();
                        break;
                    case 7:
                        ChangeModel::where(['id' => $other_id])->delete();
                        break;
                    case 8:
                        ServiceMoveModel::where(['id' => $other_id])->delete();
                        break;
                    case 10:
                        ServiceMoveModel::where(['id' => $other_id])->delete();
                        break;
                    case 11:
                        MaintenanceProjectModel::where(['wb_id' => $other_id])->delete();
                        break;
                    case 12:
                        MaintenanceProjectModel::where(['wb_id' => $other_id])->delete();
                        break;
                }
            } elseif ($type == 4) {
                OrderGoodsModel::where(['pay_id' => $pay_id, 'type' => 2])->delete();
            }
            $this->commit();
            return true;
        } catch (\Exception $exception) {
            $this->rollback();
            $this->error = '业务取消失败！';
            return false;
        }
    }

}